package top.igotcha.chapter.chapter7;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Gotcha
 * @date 2021/3/12
 * @describe
 */
@Slf4j(topic = "CachedThreadPool")
public class CachedThreadPoolTest {
    public static void main(String[] args) {
        // 自定义线程工厂
        ThreadFactory factory = new ThreadFactory() {
            AtomicInteger atomicInteger = new AtomicInteger(1);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "gotchaThread_" + atomicInteger.getAndIncrement());
            }
        };


        ExecutorService pool = Executors.newCachedThreadPool(factory);
        pool.execute(()->log.debug("1"));
        pool.execute(()->log.debug("2"));
        pool.execute(()->log.debug("3"));
    }
}
